package com.hks360.car_treasure.fragment;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.activity.ConnectEquipmentActivity;
import com.hks360.car_treasure.activity.MainActivity;
import com.hks360.car_treasure.activity.SettingActivity;
import com.hks360.car_treasure.application.MyApplication;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.car_treasure.dialog.TipDialog;
import com.hks360.car_treasure.util.MyUtil;
import com.hks360.car_treasure.widget.MeView;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;
import com.hks360.library.util.UIUtil;
import com.pgyersdk.feedback.PgyFeedback;
import com.pgyersdk.feedback.PgyFeedbackShakeManager;
import com.pgyersdk.update.PgyUpdateManager;
import com.pgyersdk.views.PgyerDialog;

/**
 * 信息-用户信息Fragment
 */
public class InfoUserFragment extends BaseFragment {
    private TextView mExitLoginTv;
    private TextView mMobileTv;
    private MeView mUpdateMv;
    private MeView mCacheMv;
    private MeView mSettingMv;
    private MeView mFeedbackMv;
    private MeView mLinkDeviceMv;
    TipDialog tipDialog;


    @Override
    public void setupView() {
        PgyFeedbackShakeManager.register(getContext());
        mExitLoginTv = UIUtil.findViewById(mRootView, R.id.user_exit_login_tv);
        mMobileTv = UIUtil.findViewById(mRootView, R.id.userinfo_mobile_tv);
        mUpdateMv = UIUtil.findViewById(mRootView, R.id.userinfo_update_mv);
        mCacheMv  = UIUtil.findViewById(mRootView, R.id.userinfo_clearcache_mv);
        mSettingMv = UIUtil.findViewById(mRootView, R.id.userinfo_setting_mv);
        mFeedbackMv = UIUtil.findViewById(mRootView, R.id.userinfo_feedback_mv);
        mLinkDeviceMv = UIUtil.findViewById(mRootView,R.id.user_info_linkdevice);
        tipDialog = new TipDialog(getActivity(),getContext());
    }

    @Override
    public void onPause() {
        super.onPause();
        PgyFeedbackShakeManager.unregister();
    }

    @Override
    public void initData() {
        mMobileTv.setText(PrefUtil.getString(getContext(), PrefKey.LOGIN_PHONE, getString(R.string.username)));
        mUpdateMv.setText(getString(R.string.version, MyUtil.getVersionInfo(getContext(),"com.hks360.car_treasure")));
    }

    @Override
    public void addListener() {
        mExitLoginTv.setOnClickListener(this);
        mUpdateMv.setOnClickListener(this);
        mCacheMv.setOnClickListener(this);
        mSettingMv.setOnClickListener(this);
        mFeedbackMv.setOnClickListener(this);
        mLinkDeviceMv.setOnClickListener(this);
    }

    @Override
    public void initView() {
        setupView();
        initData();
        addListener();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_userinfo_user,null);
        initView();
        return mRootView;
    }

    @Override
    public void viewClick(View v) {
        switch (v.getId()) {
            case R.id.user_exit_login_tv:
                exit_login();
                break;
            case R.id.userinfo_clearcache_mv:
                tipDialog.setMessage("是否清除缓存");
                tipDialog.CreateDialog(2);
                break;
            case R.id.userinfo_update_mv:
                PgyUpdateManager.register(getActivity(), MyApplication.getInstance().myUpdateManagerListener);
                if(PrefUtil.getBoolean(getContext(), PrefKey.UPDATE,false)){
                    CommonUtil.showToast(getContext(),"已经是最新版本了，无需更新");
                }
                break;
            case R.id.userinfo_setting_mv:
                CommonUtil.openActivity(getContext(), SettingActivity.class);
                break;
            case R.id.userinfo_feedback_mv:
                PgyerDialog.setDialogTitleBackgroundColor("#ff0000");
                PgyerDialog.setDialogTitleTextColor("#333333");
                PgyFeedback.getInstance().showDialog(getContext());
                break;
            case R.id.user_info_linkdevice:
                CommonUtil.openActivity(getContext(), ConnectEquipmentActivity.class);
                break;
        }
    }

    private void exit_login() {
        tipDialog.setMessage("是否退出当前账号");
        tipDialog.CreateDialog(1);
    }

}
